package com.spring.Service;

import com.spring.Entity.LogEntity;
import com.spring.Repository.LogRepository;
import com.spring.Service.Interface.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/5.
 */
@Service("logService")
@Transactional
public class LogServiceImpl implements LogService {
    @Autowired
    private LogRepository logRepository;

    @Override
    public Map<String, Object> findLogs(Integer pageNum, Integer pageSize) {
        Map response = new HashMap();
        Page page = logRepository.findAll(new PageRequest(pageNum, pageSize));
        response.put("status", "ok");
        response.put("body", page);
        response.put("totalPage", page.getTotalPages());
        response.put("currentPage", pageNum);
        return response;
    }

    //存储日志操作
    @Override
    public void log(String user, String userId, String IP, LocalDateTime time, String domain, String function) {
        LogEntity logEntity = new LogEntity();
        logEntity.setUserName(user);
        logEntity.setUserId(userId);
        logEntity.setIp(IP);
        logEntity.setFunctionTime(Timestamp.valueOf(time));
        logEntity.setOperation(function);
        logEntity.setDomain(domain);
        logRepository.saveAndFlush(logEntity);
    }

    @Override
    public Map<String, Object> findLogsAll() {
        Map response = new HashMap();
        LogEntity[] page = logRepository.findLogsAll();
        response.put("status", "ok");
        response.put("body", page);
        return response;
    }

    @Override
    public Map<String, Object> findReqLogsAll(String userName, String IP, String Oper, String timeS, String timeE) {
        Map response = new HashMap();
        userName = ((userName != null) ? "%" + userName + "%" : "");
        IP = ((IP != null) ? "%" + IP + "%" : "");
        Oper = ((Oper != null) ? "%" + Oper + "%" : "");

        Timestamp STime = null;
        Timestamp ETime = null;
        if (timeS != null) {
            STime = Timestamp.valueOf(timeS);
        } else {
            STime = Timestamp.valueOf("2000-01-01 00:00:00");
        }

        if (timeE != null) {
            ETime = Timestamp.valueOf(timeE);
        } else {
            ETime = Timestamp.valueOf("9999-12-31 23:59:59");
        }


        LogEntity[] page = logRepository.findReqLogsAll(userName, IP, Oper, STime, ETime);
        if (page.length == 0) {
            response.put("status", "datanull");
        } else {
            response.put("status", "ok");
            response.put("body", page);
        }
        return response;
    }
}
